package com.his.api.controller;

import com.his.api.service.PatientService;
import com.his.api.service.RegistrationService;
import com.his.dao.domain.Do.SelectAllRegistrationDo;
import com.his.dao.domain.dto.ListDto;
import com.his.dao.domain.dto.RegistrationDto;
import com.his.dao.domain.dto.RegistrationNurseDto;
import com.his.dao.domain.entity.Patient;
import com.his.dao.domain.entity.Registration;
import com.his.util.annotation.RequiresLogin;
import com.his.util.annotation.RequiresPermissions;
import com.his.util.result.RestResult;
import com.his.util.result.RestResultBuilder;
import com.his.util.util.JsonUtil;
import com.his.util.util.StringUtil;
import lombok.AllArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

import static com.his.util.constant.RedisConstant.PATIENT;
import static com.his.util.constant.RedisConstant.getPatientAndAuthorization;

@RestController
@RequestMapping("/registration")
@AllArgsConstructor
public class RegistrationController {
    private RegistrationService registrationService;
    private RedisTemplate redisTemplate;

    @RequiresLogin
    @RequestMapping("/all")
    public RestResult selectAllRegistration(@RequestHeader("authorization") String authorization, @RequestBody ListDto listDto) {
        return registrationService.all(authorization, listDto);
    }

    @RequiresLogin
    @RequestMapping("/history")
    public RestResult selectHistoryRegistration(@RequestHeader("authorization") String authorization, @RequestBody ListDto listDto) {
        return registrationService.history(authorization, listDto);
    }

    @RequiresLogin
    @RequestMapping("/id")
    public RestResult selectById(@RequestBody Registration registration) {
        return registrationService.selectById(registration);
    }

    @RequiresLogin
    @RequestMapping("/add")
    public RestResult addRegistration(@RequestHeader("authorization") String authorization, @RequestBody Registration registration) {
        Integer id = StringUtil.getId(PATIENT, (String) redisTemplate.opsForValue().get(getPatientAndAuthorization(authorization)));
        registration.setPatientId(id);
        registration.setCreateTime(LocalDateTime.now());
        if (registrationService.save(registration)) {
            return new RestResultBuilder().success();
        }
        return new RestResultBuilder().fail();
    }

    @RequiresLogin
    @RequestMapping("/get/code")
    public RestResult getCode(@RequestBody Registration registration) {
        return registrationService.getCode(registration);
    }

    @RequiresLogin
    @RequestMapping("/payment")
    public String payment(@RequestParam("registrationId") Integer registrationId) {
        return registrationService.payment(registrationId);
    }

    @RequiresLogin
    @RequestMapping("/at/hospital")
    public RestResult atHospital(@RequestHeader("authorization") String authorization, @RequestBody Registration registration) {
        return registrationService.atHospital(authorization, registration);
    }

    @RequiresLogin
    @RequestMapping("/test")
    public String test() {
        return "你好";
    }
}